﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Web.Services;

namespace WebTestharness
{
   
    public partial class Test : System.Web.UI.Page, IEqualityComparer<MethodInfo>
    {
        static object lastProxy;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnGetWSDL_Click(object sender, EventArgs e)
        {
            try
            {
                MemberDiv.Visible = false;
                IronTrishul.Discovery.WSDLManager manager = new IronTrishul.Discovery.WSDLManager(txtUrl.Text, "user", "password");
                dynamic Proxy = manager.GetProxy("AUTO");

                lblDynamicResult.Text = "Type Retrieved From WSDL = " + Proxy.GetType().FullName;
                linkGetWSDL.Visible = true;
                MemberDiv.Visible = true;
                lastProxy = Proxy;
            }
            catch (Exception eg)
            {
                lblDynamicResult.Text = "Bad Service...  " + eg.Message;
            }
        }
        [WebMethod]
        public static object  [] GetWebMethodNames(string wsdlUrl)
        {
            MethodInfo m = null;
            dynamic Proxy = lastProxy;
            Type serviceType = Proxy.GetType();
            var proxyMethodCollection = serviceType.GetMethods();
            var proxybaseMethodCollection = serviceType.BaseType.GetMethods();

            var nm = from M in proxyMethodCollection
                     where !proxybaseMethodCollection.Contains(M, new Test())
                     select new { Name = M.Name , ReturnType = M.ReturnParameter.ParameterType.Name  };
            
            return nm.ToArray();
        }


        [WebMethod]
        public static object []  GetWebMethodParameters(string mName)
        {
            MethodInfo m = null;
            dynamic Proxy = lastProxy;
            Type serviceType = Proxy.GetType();
            var baseTypeMethods = serviceType.BaseType.GetMethods();
            var nm = from M in serviceType.GetMethods()
                     where M.Name == mName && !baseTypeMethods.Contains<MethodInfo>(M)
                     select M;
            m = (MethodInfo)nm.ToList().First<MemberInfo>();
            

            ParameterInfo p = null ;
            var pm = from P in m.GetParameters()
                     orderby P.Position
                     select new { ParameterType = P.ParameterType.Name, ParameterName = P.Name, OutParameter = P.IsOut, Position = P.Position };

            var opr = new [] {  new { ReturnType = m.ReturnType.Name, MethodName = m.Name, BraketOpen = "(", Parameters = pm.ToArray(), BraketClose =")" }};

            return opr;
        }


        [WebMethod]
        public static object WebInvoke(string jsonParameters)
        {

            return "todo";
        }
        //protected void btnGetMembers_Click(object sender, EventArgs e)
        //{
        //    dynamic Proxy = lastProxy;
        //    try
        //    {
        //        btnGetMembers.Visible = false;
        //        Type serviceType = Proxy.GetType();
        //        lblDynamicResult.Text = "Type Retrieved From Proxy = " + serviceType.FullName;
        //        btnGetMembers.Visible = true;

        //        foreach (MethodInfo method in serviceType.GetMethods())
        //        {
        //            TableRow row_1 = new TableRow();

        //            TableCell cell_1x1 = new TableCell();
        //            Label l_1x1 = new Label();
        //            l_1x1.Text = method.Name;
        //            cell_1x1.Controls.Add(l_1x1);
        //            row_1.Cells.Add(cell_1x1);

        //            TableCell cell_1x2 = new TableCell();
        //            LinkButton l_1x2 = new LinkButton();
        //            l_1x2.Text = "Enter Data for (" + method.GetParameters().Length + ")" ;
        //            cell_1x2.Controls.Add(l_1x2);
        //            row_1.Cells.Add(cell_1x2);

        //            tblWebProxy.Rows.Add(row_1);
        //        }
        //    }
        //    catch (Exception eg)
        //    {
        //        lblDynamicResult.Text = "Bad Service...  " + eg.Message;
        //    }
        //}

        #region IEqualityComparer<MethodInfo> Members

        public bool Equals(MethodInfo x, MethodInfo y)
        {
            if (x.Name == y.Name) 
                return true;

            else
                return false;
        }

        public int GetHashCode(MethodInfo obj)
        {
            return obj.GetHashCode();
           // throw new NotImplementedException();
        }

        #endregion
    }
}